package com.heu.blood.dict.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.dict.entity.DictDepartmentEntity;
import com.heu.blood.dict.service.DictDepartmentService;
import com.heu.blood.dict.vo.DepartmentDictVo;
import com.heu.blood.globalExceptionHandler.MyException;
import com.heu.blood.dict.exception.DictExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 科别
 *
 * @author lyj
 * @email lyj@heu.com
 * @date 2023-06-16 00:26:49
 */
@RestController
@RequestMapping("dict/dictdepartment")
public class DictDepartmentController {
    @Autowired
    private DictDepartmentService dictDepartmentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "字典部门信息", operate = "分页查询部门管理列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dictDepartmentService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询树形控件
     */
    @RequestMapping("/tree")
    @ControllerLogAnnotation(module = "字典部门信息", operate = "查询树形控件")
    public R getTreeRes(@RequestParam Map<String, Object> params){
        List<DepartmentDictVo> res = dictDepartmentService.getTreeRes();
        return R.ok().put("data",res);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "字典部门信息", operate = "根据id查询部门信息")
    public R info(@PathVariable("id") Long id){
		DictDepartmentEntity dictDepartment = dictDepartmentService.getById(id);

        return R.ok().put("dictDepartment", dictDepartment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "字典部门信息", operate = "保存部门信息")
    public R save(@RequestBody DictDepartmentEntity dictDepartment){
		int flag = dictDepartmentService.saveWithCheck(dictDepartment);
		if(flag == 0){
            R.ok();
        }else if(flag == 1){
		    throw new MyException(DictExceptionEnum.EXIST_CODE);
        }else if(flag == 2){
            throw new MyException(DictExceptionEnum.SAME_CODE_PARENT_CODE);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "字典部门信息", operate = "修改部门信息")
    public R update(@RequestBody DictDepartmentEntity dictDepartment){
		dictDepartmentService.updateById(dictDepartment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "字典部门信息", operate = "删除部门信息")
    public R delete(@RequestBody Long[] ids){
		dictDepartmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
